import Taro, {Component} from '@tarojs/taro'

import {Picker, Text, View} from "@tarojs/components"
import './index.scss'
import {AtButton, AtForm, AtIcon, AtImagePicker, AtInput, AtTextarea} from "taro-ui";
import {getGlobalData} from "../../../global.d";
import {createNotice} from "../createNotice/service";
import {createHomework} from "./service";

export default class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {
            current: 0,
          selector:[['全部','进行中','已截止']],
          selectorChecked:'全部',
          title: '',
          content: '',
          sel1: 0,
          pic: [],
          picID:'',
          fileID:'',
          file:''
        }
    }
  componentWillMount(){
    this.setState({

    })
    Taro.setNavigationBarTitle({
      title:'发布作业'
    })
  }
    Submit(){
      let myDate = new Date()
      const picPath = this.state.pic[0].url
      const filePath = this.state.file
      const picCloudPath =  'homework_pic/'+myDate.getTime()+picPath.match(/\.[^.]+?$/)[0]
      const fileCloudPath = 'homework_file/'+myDate.getTime()+filePath.match(/\.[^.]+?$/)[0]
      this.setFileID(picCloudPath,filePath,picPath,fileCloudPath)
    }
  handleChange (key,value) {
    this.setState({
      [key]: value
    });
  }
  feedbackChange(event){
    this.setState({
      content: event.target.value
    })
  }
  /*获得文件临时地址存于file*/
  submitFiles() {
      wx.chooseMessageFile({
        count: 10,
        type: 'file',
        success: res =>{
          console.log('成功',res)
          let file = res.tempFiles
          this.state.file = file[0].path
          /*console.log('第二',this.state.file,'----')*/
        }
      })
  }
  /*setFileID函数*/
  async setFileID(picCloudPath,filePath,picPath,fileCloudPath){
    await this.getFileId(fileCloudPath,filePath).then(res=>{
      this.state.fileID = res
    })
    await  this.getPicId(picCloudPath,picPath).then(res=>{
      this.state.picID = res
      const user = getGlobalData('user');
      const myjson = {'user':user._id,
        'title': this.state.title,
        'content': this.state.content,
        'sel1':this.state.sel1,
        'fileID':this.state.fileID,
        'picID':this.state.picID,
        'classKey':user.userLastTimeClass.classKey

      }
      createHomework(myjson)
    })
  }
  /*异步函数，获取fileID*/
  async getFileId(cloudPath,filePath){
    let fileID = ''
    await wx.cloud.uploadFile({
      cloudPath: cloudPath ,
      filePath: filePath,
    }).then(res => {
      fileID = res.fileID
    })
    console.log('在函数中的fileID是',fileID)
    return fileID
    resolve()
  }
  /*异步函数，获取picID*/
  async getPicId(cloudPath,filePath){
    let picID = ''
    await wx.cloud.uploadFile({
      cloudPath: cloudPath ,
      filePath: filePath,
    }).then(res => {
      picID = res.fileID
    })
    console.log('在函数中的fileID是',picID)
    return picID
    resolve()
  }
  onChange = e => {
    this.setState({
      selectorChecked: this.state.selector[0][e.detail.value[0]],
      sel1: e.detail.value[0]
    })
  }
    render() {

        return (
            <View>
              {/*状态选择器*/}
              <View className='selectorChecked'>
                <Picker mode='multiSelector'
                        range={this.state.selector}
                        onChange={this.onChange}
                >
                  <View className='pickerItem'>
                    <AtIcon prefixClass='icon' value='guolv' size='25' color='#719BEA'></AtIcon>
                    <Text> {this.state.selectorChecked}</Text>
                    <View className='icon_2'>
                      <AtIcon   value='chevron-down' size='25' color='#CCCCCC'></AtIcon>
                    </View>
                  </View>
                </Picker>
              </View>
              {/*标题*/}
              <AtInput
                name='title'
                title='标题'
                type='text'
                placeholder='作业标题'
                value={this.state.title}
                onChange={this.handleChange.bind(this, 'title')}
              />
              {/*作业内容*/}
              <AtTextarea
                value={this.state.content}
                onChange={this.feedbackChange.bind(this)}
                maxLength={200}
                placeholder='作业内容...'
                count= {true}
                placeholderStyle=''
                auto-height={true}
              />
              {/*图片选择器*/}
              <AtImagePicker
                files={this.state.pic}
                onChange={this.handleChange.bind(this,'pic')}
              />
              {/*上传文件按钮*/}
              <AtButton onClick={this.submitFiles.bind(this)}>上传作业文件</AtButton>
              {/*提交Button*/}
              <AtButton onClick={this.Submit.bind(this)}>提交</AtButton>
            </View>
        )
    }

}
